$(function () {
    $.get('/eshop/order.do',{m:'findByUserID',userID:$('#userID').val()},function (data) {
        console.log($('.trade-list div'));
        data.forEach(function (e, i) {
            let str = '<div><div class="trade-list-top clear-float">\n' +
                '                <div class="trade-list-l">\n' +
                '                    <span class="trade-date">'+format(e.createTime)+'</span>\n' +
                '                    <span class="trade-no">订单号:'+e.id+'</span>\n' +
                '                </div>\n' +
                '                <a href="javascript:void(0)" onclick="deleteOrder(\''+e.id+'\')"><i class="fa fa-trash trade-list-r"></i></a>\n' +
                '            </div>'
            e.orderProducts.forEach(function (x, y) {
                str+='<div>\n' +
                    '                <ul class="clear-float">\n' +
                    '                    <li class="info">\n' +
                    '                        <img src="/eshop/Img.do?m=showImg&name='+x.product.productImage+'" />\n' +
                    '                        <p> '+x.product.productName+'</p>\n' +
                    '                    </li>\n' +
                    '                    <li class="price">￥'+x.product.price+'</li>\n' +
                    '                    <li class="num">'+x.productNum+'</li>\n' +
                    '                    <li class="real-price">￥'+(x.product.price*x.productNum)+'</li>\n' +
                    '                    <li class="operate"><a href="javascript:void(0)" onclick="toGoodsDetail(\''+x.product.id+'\')">再次购买</a></li>\n' +
                    '                </ul>\n' +
                    '            </div>'
            })
            str+='</div>'
            console.log(str);
            $('.trade-list').append(str)
        })
    })
})

//时间转换函数
function add0(m){return m<10?'0'+m:m }
function format(shijianchuo) {
//shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y+'-'+add0(m)+'-'+add0(d);
}

//删除
function deleteOrder(id) {
    if (confirm("确定删除该订单吗？")){
        $.get('/eshop/order.do',{m:'delete',id:id},function (data) {
            alert(data);
            window.location.reload()
        })
    }

}